// pages/Type/TypeDes/index.js
var common = require("../../../../public.js");
var HttpUrl = common.HttpUrl
Page({

  /**
   * 页面的初始数据
   */
  data: {
    isMore: true,
    TypeList: [],
    active: 0,
    pageNum: 0,
    id: '',
    cid: 3,

  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function(options) {
    this.setData({
      cid: options.cid,
      active: options.index,
      id:options.id
      // cid: 3,
      // active: 0,
      // id: 218,
      // tabIndex: 0
    })
    this.getTypeList();
    this.getTypeByIdData();
  },
  onChange(event) {
    this.setData({
      tabIndex: event.detail.index
    })
    var index = 0;
    for (var i = 0; i < this.data.TypeList.length; i++) {
      index = this.data.TypeList[event.detail.index].id
    }
    this.setData({
      cid: index
    }, () => {
      this.getTypeByIdData();
    })

  },
  getTypeList: function() {
    var that = this;
    wx.request({
      url: HttpUrl + '/item/category/list',
      data: {
        id: that.data.cid
      },
      method: "GET",
      header: {
        'content-type': 'application/json' // 默认值
      },
      success: function(res) {
        that.setData({
          TypeList: res.data.data
        }, () => {
          that.getTypeByIdData()
        })
      }
    })
  },
  getTypeByIdData: function() {
    var that = this
    wx.request({
      url: HttpUrl + '/item/cate/list',
      data: {
        cid: that.data.cid,
        pageNum: 1
      },
      method: "GET",
      header: {
        'content-type': 'application/json' // 默认值
      },
      success: function(res) {
        if (res.data.status == 400) {
          that.setData({
            isMore: false
          })
        } else {
          that.setData({
            isMore: true
          })
        }
        that.setData({
          TypeIdList: res.data.data
        }, () => {
          that.loadMore()
        })
      }
    })
  },
  loadMore: function(e) {
    var that = this;
    var page = that.data.pageNum + 1
    wx.request({
      url: HttpUrl + '/item/cate/list',
      data: {
        cid: that.data.id,
        pageNum: page
      },
      method: "GET",
      header: {
        'content-type': 'application/text'
      },
      success: function(res) {
        var data = res.data.data;
        if (data == "" || data == null || data == " undefined") {
          wx.showToast({
            title: '加载结束',
            icon:'none'
          })
        } else {
          wx.showLoading({
            title: '加载中...',
            mask: true
          })
          that.setData({
            TypeIdList: that.data.TypeIdList.concat(res.data.data),
            pageNum: page
          })
          wx.hideLoading();
        }
      }
    })
  }
})